package com.hymjweb.demo.hymj.basic_data_manage.xt_airport.service;

import com.hymjweb.demo.framework.cache.service.GlobalCacheService;
import com.hymjweb.demo.framework.result.MIniUIGridResultMap;
import com.hymjweb.demo.framework.sys.BaseService;
import com.hymjweb.demo.framework.sys.SysConstant;
import com.hymjweb.demo.framework.util.DataTypeUtil;
import com.hymjweb.demo.framework.util.PinyinUtil;
import com.hymjweb.demo.framework.util.util.PinYinUtils;
import com.hymjweb.demo.hymj.basic_data_manage.xt_airport.bean.XtAirportCachePojo;
import com.hymjweb.demo.hymj.basic_data_manage.xt_airport.bean.XtAirportPojo;
import com.hymjweb.demo.hymj.basic_data_manage.xt_airport.goods_site.bean.XtAirportGoodsSitePojo;
import com.hymjweb.demo.hymj.basic_data_manage.xt_airport.goods_site.detail.bean.XtAirportGoodsSiteDetailPojo;
import com.hymjweb.demo.hymj.basic_data_manage.xt_airport.goods_site.service.XtAirportGoodsSiteService;
import com.hymjweb.demo.hymj.basic_data_manage.xt_airport.mapper.XtAirportMapper;
import com.hymjweb.demo.hymj.log_manage.aop.annotation.OperationLogger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class XtAirportService extends BaseService {
    @Autowired
    private XtAirportMapper xtAirportMapper;

    @Autowired
    private XtAirportGoodsSiteService xtAirportGoodsSiteService;

    @Autowired
    private GlobalCacheService globalCacheService;

    public Object getPageList(Map map) {
        this.pageListInit(map);
        List list = xtAirportMapper.findAll(map);
        MIniUIGridResultMap resultMap = this.pageToGrid(list);
        return resultMap;
    }

    @OperationLogger(oper = SysConstant.OPER_ADD, operTitle="机场-新增" , operMemo = "", classType = XtAirportPojo.class)
    @Transactional(rollbackFor = {Exception.class})
    public int insert(XtAirportPojo pojo) throws Exception{
        if(pojo == null){
            throwException("始发机场新增参数错误!");
        }
        checkPojo(pojo);
        //检查ID重复
        XtAirportPojo dbXtAirportPojoFindObjectById = xtAirportMapper.findObjectById(pojo.getId());
        if(dbXtAirportPojoFindObjectById != null){
            throw new Exception(SysConstant.XT_DIC_ERROR_MSG.exist_id.getName());
        }
        //检查UK
        XtAirportPojo dbXtAirportPojo = xtAirportMapper.findUK(pojo.getName(),null);
        if(dbXtAirportPojo != null){
            throw new Exception(SysConstant.XT_DIC_ERROR_MSG.exist_name.getName());
        }

        pojo.setCamelCharPinyin(PinyinUtil.getPinYinFirst(pojo.getFullPinyin()));

        Date currentDate = this.getSysTime();
        String czyId = this.getLoginCzyId();

        pojo.setCreateTime(currentDate);
        pojo.setCreateUserId(czyId);
        pojo.setUpdateTime(currentDate);
        pojo.setUpdateUserId(czyId);
        int flag = xtAirportMapper.insert(pojo);
        if(flag > SysConstant.INT_ZERO){
            globalCacheService.loadXtAirportCache();
        }
        return flag;
    }

    @OperationLogger(oper = SysConstant.OPER_UPDATE, operTitle="机场管理-更新" , operMemo = "", classType = XtAirportPojo.class)
    @Transactional(rollbackFor = {Exception.class})
    public int update(XtAirportPojo pojo) throws Exception{
        if(pojo == null){
            throwException("始发机场更新参数错误!");
        }
        checkPojo(pojo);
        //检查UK
        XtAirportPojo dbXtAirportPojo = xtAirportMapper.findUK(pojo.getName(),pojo.getId());
        if(dbXtAirportPojo != null){
            throwException("存在同名机场，始发机场更新失败!");
        }
        pojo.setCamelCharPinyin(PinyinUtil.getPinYinFirst(pojo.getFullPinyin()));
        pojo.setUpdateTime(this.getSysTime());
        pojo.setUpdateUserId(this.getLoginCzyId());
        int flag = xtAirportMapper.update(pojo);
        if(flag > SysConstant.INT_ZERO){
            globalCacheService.loadXtAirportCache();
        }
        return flag;
    }

    /**
     * 检查必输项
     * @param pojo
     */
    private void checkPojo(XtAirportPojo pojo) throws Exception{
        DataTypeUtil.isEmptyStrThenThrow(pojo.getId(), SysConstant.XT_DIC_ERROR_MSG.no_id.getName());
        DataTypeUtil.isEmptyStrThenThrow(pojo.getName(), SysConstant.XT_DIC_ERROR_MSG.no_name.getName());
    }

    public List<XtAirportCachePojo> findCacheAll(Map map){
        List<XtAirportCachePojo> pojos = xtAirportMapper.findCacheAll(map);
        return pojos;
    }

    @OperationLogger(oper = SysConstant.OPER_DELETE, operTitle="机场-删除" , operMemo = "", classType = XtAirportPojo.class)
    @Transactional(rollbackFor = {Exception.class})
    public int delete(String id) throws Exception{
        if(DataTypeUtil.isEmptyStr(id)){
            throwException("始发机场删除参数错误");
        }
        // keyAirportId
        Map<String,Object> param = new HashMap<>();
        param.put("keyAirportId",id);
        List<XtAirportGoodsSitePojo> xtAirportGoodsSitePojoList = xtAirportGoodsSiteService.findCacheAll(param);
        if(xtAirportGoodsSitePojoList != null && xtAirportGoodsSitePojoList.size() != 0){
            throwException("待删除的始发机场下存在货站，不能删除始发机场!");
        }
        int flag = xtAirportMapper.delete(id);
        if(flag > SysConstant.INT_ZERO){
            globalCacheService.loadXtAirportCache();
        }
        return flag;
    }


    /**
     * 根据key条件查询，这是用于页上的autocomplete下拉列表
     * @param key
     * @return
     */
    public List<XtAirportPojo> queryAirPortByKey(String key) {
        if(!DataTypeUtil.isEmptyStr(key)){
            return xtAirportMapper.queryAirPortByKey(key);
        }
       return null;
    }
}
